<template>
  <el-dialog :title="!dataForm.interestId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="兴趣名称" prop="interestName">
        <el-input v-model="dataForm.interestName" placeholder="兴趣名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        interestId: 0,
        interestName: "",
        parentId: "",
        sort: ""
      },
      dataRule: {
        interestName: [
          { required: true, message: "兴趣名称不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(data) {
      console.log(data);
      this.dataForm.interestId = data.interestId;
      this.dataForm.parentId = data.parentId || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.interestId) {
          this.$http({
            url: this.$http.adornUrl(
              `/admin/interest/info/${this.dataForm.interestId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.interestName = data.interest.interestName;
              this.dataForm.parentId = data.interest.parentId;
              this.dataForm.sort = data.interest.sort;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/admin/interest/${!this.dataForm.interestId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              interestId: this.dataForm.interestId || undefined,
              interestName: this.dataForm.interestName,
              parentId: this.dataForm.parentId,
              showStatus: 1,
              sort: this.dataForm.sort
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
